package com.atfcm.queue;

import java.util.*;

/**
 * @Author 96534
 * @create 2025/3/28 17:59
 */
public class T_451_根据字符出现频率排序 {
    public static String frequencySort(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0)+1);
        }

        ArrayList<Map.Entry<Character,Integer>> objects = new ArrayList<>(map.entrySet());
        Collections.sort(objects,(a1,a2)-> a1.getValue()-a2.getValue());

        StringBuilder sb = new StringBuilder();
        for (Map.Entry<Character,Integer> entry : objects) {
            for (int i = 0; i < entry.getValue(); i++) {
                sb.append(entry.getKey());
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        frequencySort("fa");
    }
}
